var util = require('../../utils/util.js')
var app = getApp()
Page({
    data: {
        postList: {},
        requestUrl:'',
        topUrl:'',
        topList: {},
        page: 1,
        isEmpty: true,
        active: 0
    },
    //获取首页博客数据列表、置顶博客信息
    onLoad() {
        var topUrl = app.globalData.apiBase +
            "/blog/getTop";
        this.data.topUrl=topUrl;
        util.ajax(topUrl, "get", null, this.processTopData)
      var requestUrl=app.globalData.apiBase+ "/blog/getBlogList";
        this.data.requestUrl = requestUrl;
        var dataUrl=requestUrl+'?pageNum=0&pageSize=5'
        util.ajax(dataUrl,'get',null,this.processData)
    },
    processData: function (indexData) {
        var totalData = {};
        //如果要绑定新加载的数据，那么需要同旧有的数据合并在一起
        if (!this.data.isEmpty) {
            totalData = this.data.postList.concat(indexData.list);
        }
        else {
            totalData = indexData.list;
            this.data.isEmpty = false;
        }
        this.setData({
            postList: totalData
        });

        this.data.page += 1;
        wx.hideNavigationBarLoading();
        //小程序提供的api，通知页面停止下拉刷新效果 如果发现进入刷新状态，无法停止，可以使用这个api
        wx.stopPullDownRefresh();
    },

    //规范化调用参数
    processTopData(data){
        this.setData({
            topList: data.list
        });
    },
    onSwiperTap: function (event) {
        // target 和currentTarget
        // target指的是当前点击的组件 和currentTarget 指的是事件捕获的组件
        // target这里指的是image，而currentTarget指的是swiper
        //采用事件冒泡的方式捕获点击事件：参考：https://baijiahao.baidu.com/s?id=1575515058154089&wfr=spider&for=pc
        var id = event.target.dataset.id;
        wx.navigateTo({
            url: "/pages/post-detail/post-detail?id=" + id + "&title=主页"
        })
    },
    onPostTap: function (event) {
        var id = event.currentTarget.dataset.id;
        var title = event.currentTarget.dataset.title;
        wx.navigateTo({
            url: "/pages/post-detail/post-detail?id=" + id + "&title=" + title
        })
    },
    //下拉刷新的时候触发
    onPullDownRefresh: function (event) {
        var refreshUrl = this.data.requestUrl +
            "?pageNum=0&pageSize=5";
        this.data.techList = {};
        this.data.isEmpty = true;
        this.data.page = 1;
        util.ajax(refreshUrl, "get", null, this.processData);

        var topUrl = this.data.topUrl
        util.ajax(topUrl, "get", null, this.processTopData)
        wx.showNavigationBarLoading();
    },

    // 上滑加载更多数据
    onReachBottom: function (event) {
        var nextUrl = this.data.requestUrl +
            "?pageNum=" + this.data.page + "&pageSize=5";
        util.ajax(nextUrl, "get", null, this.processData)
        wx.showNavigationBarLoading()
    },
    onTabChange(event) {
        let turnFlag=event.detail
        var url=''
        console.log(turnFlag)
        switch(turnFlag)
        {
            case 0:
                url='/pages/posts/post'
                break;
            case 1:
                url='/pages/special/special'
                break;
            case 2:
                url='/pages/security/security'
                break;
            case 3:
                url='/pages/me/me'
                break;

        }
        //这里是一个坑：微信自从改版之后redirectTo和navigateTo均不可以跳转有tabbar的页面了
        //为了防止重定向的时候出现闪屏效果，先让当前页面出栈，然后在重定向
        getCurrentPages().pop()
        wx.redirectTo({
            url: url
        })

    },
})

